function uploadAvatar(uploadType,memberId){
	
	if(IsNull(memberId)){
		memberId="";
	}
	$("#cut_avatar").empty();
	$("#cut_avatar").hide();
	$("#pop_avatar_div").load("avatarPop.htm?type="+uploadType+"&memberId="+memberId,function(){
		$("#pop_avatar_div").show();
		$("#pop_avatar_div").bgiframe();
	});
	
	
}

function avatarPopSetTab(type,uploadType,memberId){
	if(type == 1){
		setTab('pop_avatar',type,2);
	}else{
		$("#con_pop_avatar_"+type).load("avatarPopTab"+type+".htm?uploadType="+uploadType+"&memberId="+memberId,function(){
		setTab('pop_avatar',type,2);
		});
	}
	
}

function changAlbum(value,type,memberId){
	$("#avatar_pop_tab2_list").load("avatarPopTab2List.htm?albumId="+value+"&uploadType="+type+"&memberId="+memberId);
}


function uploadAvatarGoOn(type,memberId){
	
	var filepath=$("#avatarUploadFile").val(); 
	var extStart=filepath.lastIndexOf("."); 
	var ext=filepath.substring(extStart,filepath.length).toUpperCase(); 
	if(ext!=".PNG"&&ext!=".JPG"&&ext!=".JPEG"){ 
		alert("图片限于png,jpeg,jpg格式"); 
		return ; 
	}
	
$("#avatarUpload").ajaxSubmit({
		dataType : 'json',
		cache : false,
		success : function(data) {
			if(data.sizeAllow==true){
				$("#cut_avatar_1").empty();
				$("#cut_avatar_1").load("avatarCutDiv.htm",{
					height:data.height,
					width:data.width,
					imageUrl:data.imageUrl,
					type:type,
					photoType:1,
					memberId:memberId
				});
			}else{
				alert("照片大小不能超过2M!");
			}
			
			
		},
		error : function(err) {
			alert(err);
		}
	});
	return false;
}

function selectPhoto(photoId,photoUrl,type,memberId){
	$("#pop_avatar_div").empty();
	$("#pop_avatar_div").hide();
	$.post("selectPhoto.htm",{
		photoId:photoId,
		photoUrl:photoUrl
	},function(data){
		$("#cut_avatar").load("avatarCutDiv.htm",{
			height:data.height,
			width:data.width,
			imageUrl:data.imageUrl,
			type:type,
			photoType:2,
			memberId:memberId
		},function(){
			$("#cut_avatar").bgiframe();
		});
	},'json');
	
}

function cutAvatar(type,photoType,memberId){
	$.post("avatarCut.htm",{
		'srcImg':$("#src_image").attr("url"),
		'x':$('#x').val(),
		'y':$('#y').val(),
		'x2':$('#x2').val(),
		'y2':$('#y2').val(),
		'w':$('#w').val(),
		'h':$('#h').val(),
		'realWidth':$("#src_image").width(),
		'realHeight':$("#src_image").height(),
		type:type,
		photoType:photoType,
		memberId:memberId
	},function(data){
		if(type==1){
			$("#avatar_member").attr("src",data);
			refreshParentsAvatar();
		}else if(type==2){
			$("#avatar_family").attr("src",data);
			$("#familyAvatar").attr("src",data);
		}else if(type==3){
			refreshBabysAvatar();
		}
		if(photoType == '1'){
			$("#pop_avatar_div").empty();
			$("#pop_avatar_div").hide();
		}else{
			$("#cut_avatar").empty();
			$("#cut_avatar").hide();
		}
		
		
	});
}

function cutAvatarCancel(photoType){
	/*
	if(photoType=='1'){
		$.post("cutAvatarCancel.htm",{'srcImg':$("#src_image").attr("url")},function(data){
			if(data=='success'){
				$("#cut_avatar").empty();
				$("#cut_avatar").hide();
			}
		});
	}else{
*/		
		$("#cut_avatar").empty();
		$("#cut_avatar").hide();
//	}
	
	
}

function showCoords(coords,realWidth,realHeight){
	$('#x').val(coords.x);
	$('#y').val(coords.y);
	$('#x2').val(coords.x2);
	$('#y2').val(coords.y2);
	$('#w').val(coords.w);
	$('#h').val(coords.h);
	showPreview(coords,realWidth,realHeight);
}

function showPreview(coords,realWidth,realHeight){
	if (parseInt(coords.w) > 0){
		var rx = 120 / coords.w;
		var ry = 120 / coords.h;
		
		var rx2 = 34 / coords.w;
		var ry2 = 34 / coords.h;

		$('#preview1').css({
			width: Math.round(rx * realWidth) + 'px',
			height: Math.round(ry * realHeight) + 'px',
			marginLeft: '-' + Math.round(rx * coords.x) + 'px',
			marginTop: '-' + Math.round(ry * coords.y) + 'px'
		});
		
		$('#preview2').css({
			width: Math.round(rx2 * realWidth) + 'px',
			height: Math.round(ry2 * realHeight) + 'px',
			marginLeft: '-' + Math.round(rx2 * coords.x) + 'px',
			marginTop: '-' + Math.round(ry2 * coords.y) + 'px'
		});
	}
}

function jcropAvatar(width,height,url){
	$('#src_image').attr("src",url+"?tempid="+Math.random());
	$('#preview1').attr("src",url+"?tempid="+Math.random());
	$('#preview2').attr("src",url+"?tempid="+Math.random());
	$('#src_image').Jcrop({aspectRatio:1,
						   onChange:function(coords){showCoords(coords,width,height);},
						   onSelect:function(coords){showCoords(coords,width,height);},
						   setSelect:[0,0,120,120],
						   allowSelect:	false
							}
						);
}


